﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NotifyServices
{
    public partial class SimpleTask : Form
    {
        public List<string> ServiceList = new List<string> { "MSSQLSERVER", "MSSQL$SQLEXPRESS", "MSSQL$SQL2008" };
       
        public SimpleTask()
        {
            InitializeComponent();
            ServiceList.ForEach(s => LoadControl(s));

        }

        public void LoadControl(string ServiceName)
        {
            ServiceControllerButton btnAction = new ServiceControllerButton(".", ServiceName);
            Label lblInfo = new Label() { Text = btnAction.Service.Service};
            btnAction.Dock = DockStyle.Left;
            btnAction.FlatStyle = FlatStyle.Flat;

            btnAction.Click += new EventHandler(
                delegate(object sender, EventArgs e) { 
                    (sender as ServiceControllerButton).UpdateService(); 
                });
            btnAction.Finished += new ServiceControllerButton.OnFinish(
                delegate(ServiceResponse r){
                    lblInfo.Text= r.Service + ":" + r.CurrentStatus;
                });
            lblInfo.Dock = DockStyle.Fill;
            Panel pnl = new Panel();
            pnl.Dock = DockStyle.Top;

            pnl.Controls.Add(lblInfo);
            pnl.Controls.Add(btnAction);
            pnl.Height = 40;
            this.Controls.Add(pnl);
        }
    }
}
